package com.itrh.auth.service.impl;

import com.itrh.auth.domain.*;
import com.itrh.auth.dto.RoleDTO;
import com.itrh.auth.mapper.PermissionMapper;
import com.itrh.auth.mapper.RoleMapper;
import com.itrh.auth.mapper.RoleMenuMapper;
import com.itrh.auth.mapper.RolePermissionMapper;
import com.itrh.auth.service.IRoleService;
import com.itrh.base.service.impl.BaseServiceImpl;
import com.itrh.org.util.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XHU_WHY
 * @since 2024-04-07
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private PermissionMapper permissionMapper;
    @Resource
    private RolePermissionMapper rolePermissionMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;
    @Override
    public Result saveOrUpdate(Role role) {
        try {
            if (role.getId() == null) {
                roleMapper.insert(role);
            } else {
                roleMapper.update(role);
            }
            return Result.success();
        } catch (Exception e) {
            return Result.error("保存失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addOrUpdate(RoleDTO dto) {
        try {
            Role role = dto.getRole();
            if (role.getId() == null) {
                roleMapper.insert(role);
            } else {
                roleMapper.update(role);
            }
            // 删除之前的权限
            rolePermissionMapper.deleteByRoleId(role.getId());
            // 查询所有权限
            List<Permission> permissions = permissionMapper.selectByIds(dto.getPermissionIds());
            List<RolePermission> list = permissions.stream().map(permission -> {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(role.getId());
                rolePermission.setPermissionId(permission.getId());
                rolePermission.setPermissionSn(permission.getSn());
                return rolePermission;
            }).collect(Collectors.toList());
            rolePermissionMapper.saveBatch(list);
            // 删除之前的菜单
            roleMenuMapper.deleteByRoleId(role.getId());
            // 保存菜单
            List<Long> menuIds = dto.getMenuIds();
            List<RoleMenu> menus = menuIds.stream().map(menuId -> {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(role.getId());
                roleMenu.setMenuId(menuId);
                return roleMenu;
            }).collect(Collectors.toList());
            roleMenuMapper.saveBatch(menus);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("保存失败");
        }
    }

    @Override
    public Result getPermissionIds(Long id) {
        try {
            List<Long> ids = rolePermissionMapper.selectByRoleId(id);
            return Result.success(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取失败");
        }
    }

    @Override
    public Result getMenuByRoleId(Long id) {
        try {
            List<Menu> menus = roleMenuMapper.selectByRoleId(id);
            return Result.success(menus);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取失败");
        }
    }
}
